﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using static System.Console;

namespace MulticastDelegateWithIteration
{
    class Program
    {
        static void one()
        {
            WriteLine("one");
            //抛出Exception异常
            throw new Exception("Error in one");
        }
        static void two()
        {
            WriteLine("two");
        }
        static void Main(string[] args)
        {
            Action dl = one;
            dl += two;
            //第一个方法one抛出异常，委托迭代停止，第二个方法two不会被调用
            //try
            //{
            //    d1();
            //}
            //catch (Exception)
            //{
            //    WriteLine("Exception caught");
            //}

            //调用委托类GetInvocationList方法，返回Delegate对象数组
            //即使捕获异常后会继续迭代下一个方法
            Delegate[] delegates = dl.GetInvocationList();
            foreach (Action d in delegates)
            {
                try
                {
                    d();
                }
                catch (Exception)
                {
                    WriteLine("Exception caught");
                }
            }
        }
    }
}
